# coding: utf-8
import codecs
from hashlib import blake2b as hash_
import time,threading
import wx
import math
import os,sys
import sqlite3
import logging
from threading import Thread
from wx.lib.pubsub import pub

root=os.path.dirname(os.path.realpath(__file__))
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s -%(levelname)s -%(process)d -%(module)s -%(funcName)s -%(message)s')
info={"ThreadRun":False,
    "dircount":0,"filecount":0,
    "filelist":[],'complete':0,
    "path":"",'root':None}
nowtime=time.time()
filelist=[]
newfilelist=[]
errorfilelist=[]
db=None
logfilename="blake2.log"

logging.debug(nowtime)

###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):
    
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 640,480 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        self.m_statusBar1 = self.CreateStatusBar( 1, wx.STB_SIZEGRIP, wx.ID_ANY )
        self.Centre( wx.BOTH )
        
    def __del__( self ):
        pass
    

###########################################################################
## Class MyPanel1
###########################################################################

class MyPanel1 ( wx.Panel ):
        
    def __init__( self, parent ):
        wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 640,480 ), style = wx.TAB_TRAVERSAL )
        
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        
        sbSizer1 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"选择目录" ), wx.VERTICAL )
        
        self.m_dirPicker1 = wx.DirPickerCtrl( sbSizer1.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a folder", wx.DefaultPosition, wx.DefaultSize, wx.DIRP_DEFAULT_STYLE )
        sbSizer1.Add( self.m_dirPicker1, 0, wx.ALL|wx.EXPAND, 5 )
        
        
        bSizer1.Add( sbSizer1, 0, wx.EXPAND|wx.ALL, 5 )
        
        sbSizer3 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"选项" ), wx.VERTICAL )
        
        bSizer3 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_checkBox1 = wx.CheckBox( sbSizer3.GetStaticBox(), wx.ID_ANY, u"添加记录", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer3.Add( self.m_checkBox1, 0, wx.ALL, 5 )
        
        self.m_staticline1 = wx.StaticLine( sbSizer3.GetStaticBox(), wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer3.Add( self.m_staticline1, 0, wx.EXPAND |wx.ALL, 5 )
        
        self.m_checkBox2 = wx.CheckBox( sbSizer3.GetStaticBox(), wx.ID_ANY, u"校验签名", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer3.Add( self.m_checkBox2, 0, wx.ALL, 5 )
        
        
        sbSizer3.Add( bSizer3, 1, wx.EXPAND, 5 )
        
        
        bSizer1.Add( sbSizer3, 0, wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, 5 )
        
        sbSizer2 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"完成" ), wx.VERTICAL )
        
        self.m_gauge5 = wx.Gauge( sbSizer2.GetStaticBox(), wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
        self.m_gauge5.SetValue( 0 ) 
        sbSizer2.Add( self.m_gauge5, 0, wx.ALL|wx.EXPAND, 5 )
        
        self.m_gauge6 = wx.Gauge( sbSizer2.GetStaticBox(), wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
        self.m_gauge6.SetValue( 0 ) 
        sbSizer2.Add( self.m_gauge6, 0, wx.ALL|wx.EXPAND, 5 )
        
        self.m_staticText1 = wx.StaticText( sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText1.Wrap( -1 )
        sbSizer2.Add( self.m_staticText1, 0, wx.ALL|wx.EXPAND, 5 )
        
        
        bSizer1.Add( sbSizer2, 0, wx.EXPAND|wx.ALL, 5 )
        
        self.m_textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE )
        bSizer1.Add( self.m_textCtrl1, 1, wx.ALL|wx.EXPAND, 5 )
        
        bSizer7 = wx.BoxSizer( wx.HORIZONTAL )
        
        
        bSizer7.Add( ( 0, 0), 1, wx.EXPAND, 5 )
        
        self.m_button1 = wx.Button( self, wx.ID_ANY, u"生成", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer7.Add( self.m_button1, 0, wx.ALL, 5 )
        
        self.m_button3 = wx.Button( self, wx.ID_ANY, u"校验", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer7.Add( self.m_button3, 0, wx.ALL, 5 )
        
        self.m_button2 = wx.Button( self, wx.ID_ANY, u"退出", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer7.Add( self.m_button2, 0, wx.ALL, 5 )
        
        
        bSizer1.Add( bSizer7, 0, wx.EXPAND, 5 )
        
        
        self.SetSizer( bSizer1 )
        self.Layout()
        
        # Connect Events
        self.m_dirPicker1.Bind( wx.EVT_DIRPICKER_CHANGED, self.onDirC )
        self.m_button1.Bind( wx.EVT_BUTTON, self.onStart )
        self.m_button2.Bind( wx.EVT_BUTTON, self.onExit )
        self.m_button3.Bind( wx.EVT_BUTTON, self.onVerify )
    

        pub.subscribe(self.updateDisplay, "update")
        pub.subscribe(self.up, "up")
        
    def __del__( self ):
        pass

    def up(self,msg):
        self.m_staticText1.SetLabel("目录总计：%s   文件总计：%s  已完成：%s 时间：%2.2f" % (msg['dircount'],msg["filecount"],msg['complete'],time.time()-nowtime))
        w.m_statusBar1.SetStatusText("%s" % msg["path"], 0)
        if(msg['filecount']==0):
            t=0
        else:  
            t=math.ceil(msg['complete']/msg['filecount']*100)
        self.m_gauge5.SetValue(t)
        if (msg["ThreadRun"]):
            self.m_button1.Disable()
            self.m_button3.Disable()
        else:
            self.m_button1.Enable()
            self.m_button3.Enable()

            

    def updateDisplay(self,msg):
        t = msg
        self.m_staticText1.SetLabel("目录总计：%s   文件总计：%s  已完成：%s 时间：%2.2f" % (info['dircount'],info["filecount"],info['complete'],time.time()-nowtime))
        if isinstance(t, int):#如果是数字，说明线程正在执行，显示数字
            #self.m_staticText2.SetLabel("%s%%" % t)
            self.m_gauge6.SetValue(t)
        else:#否则线程未执行，将按钮重新开启
            #self.m_staticText2.SetLabel("%s" % t)
            self.m_button1.Enable()
        self.m_staticText1.SetLabel("目录总计：%s   文件总计：%s  已完成：%s 时间：%2.2f" % (info['dircount'],info["filecount"],info['complete'],time.time()-nowtime))
    # Virtual event handlers, overide them in your derived class
    def onDirC( self, event ):
        global info

        info['root']=event.GetPath()
        print(info['root'])
        ScanThread()
        CheckFile()
        event.Skip()
    
    def onStart( self, event ):
        self.m_button2.SetLabel("暂停")
        HashThread()
        event.Skip()
    
    def onExit( self, event ):
        if(info["ThreadRun"]==True):
            info["ThreadRun"]=False
            self.m_button2.SetLabel("退出")
        else:
            dlg = wx.MessageDialog(None, u"确定退出？", u"确定退出", wx.YES_NO | wx.ICON_QUESTION)
            if dlg.ShowModal() == wx.ID_YES:
                if(db!=None):
                    db.close()
                sys.exit(0)
            dlg.Destroy()
        event.Skip()
    def onVerify(self,event):
        global db
        w1=MyFrame2(w)
        p1=MyPanel2(w1)
        w1.Show()
        event.Skip()


###########################################################################
## Class MyFrame2
###########################################################################
class MyFrame2 ( wx.Frame ):
    
	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

		self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )


		self.Centre( wx.BOTH )

		# Connect Events
		self.Bind( wx.EVT_CLOSE, self.MyFrame2OnClose )

	def __del__( self ):
		pass


	# Virtual event handlers, overide them in your derived class
	def MyFrame2OnClose( self, event ):
		event.Skip()


###########################################################################
## Class MyPanel2
###########################################################################

import wx.grid

###########################################################################
## Class MyPanel2
###########################################################################

class MyPanel2 ( wx.Panel ):

	def __init__( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL, name = wx.EmptyString ):
		wx.Panel.__init__ ( self, parent, id = id, pos = pos, size = size, style = style, name = name )

		bSizer4 = wx.BoxSizer( wx.VERTICAL )

		bSizer5 = wx.BoxSizer( wx.HORIZONTAL )

		self.m_treeCtrl1 = wx.TreeCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 200,-1 ), wx.TR_DEFAULT_STYLE )
		bSizer5.Add( self.m_treeCtrl1, 0, wx.ALL|wx.EXPAND, 5 )

		self.m_grid1 = wx.grid.Grid( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )

		# Grid
		self.m_grid1.CreateGrid( 50, 3 )
		self.m_grid1.EnableEditing( True )
		self.m_grid1.EnableGridLines( True )
		self.m_grid1.EnableDragGridSize( False )
		self.m_grid1.SetMargins( 0, 0 )

		# Columns
		self.m_grid1.EnableDragColMove( False )
		self.m_grid1.EnableDragColSize( True )
		self.m_grid1.SetColLabelSize( 30 )
		self.m_grid1.SetColLabelAlignment( wx.ALIGN_CENTER, wx.ALIGN_CENTER )

		# Rows
		self.m_grid1.EnableDragRowSize( True )
		self.m_grid1.SetRowLabelSize( 80 )
		self.m_grid1.SetRowLabelAlignment( wx.ALIGN_CENTER, wx.ALIGN_CENTER )

		# Label Appearance

		# Cell Defaults
		self.m_grid1.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
		bSizer5.Add( self.m_grid1, 1, wx.ALL, 5 )


		bSizer4.Add( bSizer5, 1, wx.EXPAND, 5 )

		bSizer6 = wx.BoxSizer( wx.HORIZONTAL )


		bSizer6.Add( ( 0, 0), 1, wx.EXPAND, 5 )

		self.m_button6 = wx.Button( self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
		bSizer6.Add( self.m_button6, 0, wx.ALL, 5 )

		self.m_button7 = wx.Button( self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
		bSizer6.Add( self.m_button7, 0, wx.ALL, 5 )

		self.m_button8 = wx.Button( self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
		bSizer6.Add( self.m_button8, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )


		bSizer4.Add( bSizer6, 0, wx.EXPAND|wx.ALIGN_RIGHT, 5 )

        
		self.SetSizer( bSizer4 )
		self.Layout()

	def __del__( self ):
		pass





app=wx.App()
w=MyFrame1(None)
p=MyPanel1(w)

w.Show()
app.MainLoop()